export const base = {
    data () {
        return {
            modules:'news',
            modelAdd:false,
            type:1, //分类模块
            editId : '', //单条修改
            commentNum:{
                num:0
            },
            commenTotal : 0, //评论条数
            tree:[],//树形菜单
            data: [],
            total:0,
            filter:{
               page:1,
               rows:5,
               name:'',
               cateId:''
            },
            ids:[]
        }
    },
    methods: {
        //获得树形菜单的数据
        getTree(){
            var bodyString = JSON.stringify(this.filter);
            fetch(`http://localhost:3000/cate/list/${this.type}`)
            .then(res=>{
                return res.json();
            }).then(data=>{
                 this.tree = data;
                 this.total = data.total;
                //console.log(data)
            })
        },
        onSelectChange(event){
            console.log(event[0]._id)
            this.filter.cateId = event[0]._id;
            this.getData()
        },
        onSelectionChange(selection){
            var selectionIds = [];
            for(let i = 0;i<selection.length;i++){
                selectionIds.push(selection[i]._id)
            }
            this.ids = selectionIds;
            console.log(selectionIds)
        },

        //删除多条数据
        removes(){
            this.$Modal.confirm({
                title: '确认删除对话框',
                content: '<p>是否删除多条</p>',
                onOk: () => {
                    fetch(`http://localhost:3000/${this.modules}/removes`,{
                        method:'post',
                        headers:{
                            'Content-Type':'application/json'
                        },
                        body : JSON.stringify({ids : this.ids.toString() })
                        }).then(res=>{
                        return res.json()
                        }).then(data=>{
                        this.$Message.info('删除成功');
                        this.commentNum.num -= this.ids.length;
                        fetch(`http://localhost:3000/news/data/${this.filter.cateId}`,{
                            method:'put',
                            headers:{
                                'Content-Type':"application/json"
                            },
                            body:JSON.stringify(this.commentNum)
                        ,}).then(res=>{
                            return res.json();
                        }).then(data=>{
                            console.log(data);
                        });
                        this.getData();
                        })
                }
            });
        },
        seachData(){
            this.getData()
        },
        onPagesizeChange(pagesize){
            this.filter.rows = pagesize;
            this.getData();
        },
        onPage(page){
            this.filter.page = page;
            this.getData();
        },
        //判断有没有cateId 没有则提示无法添加
        addMessage(){
            if(this.filter.cateId !== ''){
                this.modelAdd = true;
            }else{
                this.$Message.error('请选择分类，否则无法添加新闻')
            }
        },

        //点击按钮，确定新增数据还是修改数据
        handleSubmit (name) {
           
            this.$refs[name].validate((valid) => {
                //console.log(this.filter)
                if (valid) {
                    if(this.editId.length > 0){
                        //修改
                        this.$Message.success('修改成功!');
                        console.log(this.formValidate)
                        fetch(`http://localhost:3000/${this.modules}/data/${this.editId}`,{
                                method:'put',
                                headers:{
                                    'Content-Type':"application/json"
                            },
                                body:JSON.stringify(this.formValidate)
                        }).then(res=>{
                                return res.json()
                        }).then(data=>{
                                this.modelAdd = false;
                                this.getData();
                                Object.assign(
                                    this.$data.formValidate,
                                    this.$options.data().formValidate
                                );
                        })
                    }else{
                        //新增
                        this.formValidate.cateId = this.filter.cateId;
                        this.$Message.success('添加成功!');
                        fetch(`http://localhost:3000/${this.modules}/data`,{
                                method:'post',
                                headers:{
                                    'Content-Type':"application/json"
                            },
                                body:JSON.stringify(this.formValidate)
                            }).then(res=>{
                                return res.json()
                            }).then(data=>{
                                this.modelAdd = false;
                                this.commentNum.num ++;
                                console.log(this.commentNum.num);
                                fetch(`http://localhost:3000/news/data/${this.filter.cateId}`,{
                                method:'put',
                                headers:{
                                    'Content-Type':"application/json"
                                },
                                body:JSON.stringify(this.commentNum)
                                }).then(res=>{
                                    return res.json();
                                }).then(data=>{
                                    console.log(data);
                                });
                                this.getData();
                                Object.assign(
                                    this.$data.formValidate,
                                    this.$options.data().formValidate
                                );
                            })
                    }                       
                } else {
                    this.$Message.error('添加失败!');
                }
            })
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        },   

        //修改单挑数据
        editIdata (id) {
            //console.log(id)
            fetch(`http://localhost:3000/${this.modules}/data/${id}`)
            .then(res=>{
                return res.json();
            }).then(data=>{
                console.log(data);
                this.modelAdd = true;
                this.formValidate = data;
                this.editId = id;
            })
        },
        //删除指定单条数据
        remove (id) {
            this.$Modal.confirm({
                title: '确认删除对话框',
                content: '<p>是否删除</p>',
                onOk: () => {
                    fetch(`http://localhost:3000/${this.modules}/data/${id}`,{
                      method:'delete'
                    }).then(res=>{
                      return res.json()
                    }).then(data=>{
                      this.$Message.info('删除成功');
                      this.commentNum.num --;
                      fetch(`http://localhost:3000/news/data/${this.filter.cateId}`,{
                        method:'put',
                        headers:{
                            'Content-Type':"application/json"
                        },
                        body:JSON.stringify(this.commentNum)
                        }).then(res=>{
                            return res.json();
                        }).then(data=>{
                            console.log(data);
                        });
                      this.getData();
                    })
                }
            });
        },
        //获取不同模块的数据
        getData(){
            this.filter.cateId = this.$router.history.current.query.newsId
            var bodyString = JSON.stringify(this.filter)
            fetch(`http://localhost:3000/${this.modules}/list`,{
            method:'post',
            headers:{
              'Content-Type':"application/json"
            },
            body:bodyString
            }).then(res=>{
            return res.json();
            }).then(data=>{
                //console.log(data)
                this.data = data.rows;
                this.total = data.total;
                this.commentNum.num = data.total;
                console.log(this.commentNum.num);
          })
        }
    },
    //钩子函数
    created(){
      this.getData();
    }
}
